﻿using System;
using System.Collections.Generic;
using EP.Utilities;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace EP.IoC {
	public sealed class IoCHelper : Singleton<IoCHelper, IServiceLocator>, IServiceLocator {
		private IUnityContainer _container;

		private IoCHelper() {
			_container = new UnityContainer()
				//.AddNewExtension<EnterpriseLibraryCoreExtension>()	//this adds Enterprise Library Objects to our container
				.LoadConfiguration();
		}

		#region IServiceLocator Members
		public IEnumerable<TService> GetAllInstances<TService>() {
			return _container.ResolveAll<TService>();
		}
		public IEnumerable<object> GetAllInstances(Type serviceType) {
			return _container.ResolveAll(serviceType);
		}
		public TService GetInstance<TService>(string key) {
			return _container.Resolve<TService>(key);
		}
		public TService GetInstance<TService>() {
			return _container.Resolve<TService>();
		}
		public object GetInstance(Type serviceType, string key) {
			return _container.Resolve(serviceType, key);
		}
		public object GetInstance(Type serviceType) {
			return _container.Resolve(serviceType);
		}
		#endregion

		#region IServiceProvider Members
		public object GetService(Type serviceType) {
			return _container.Resolve(serviceType);
		}
		#endregion
	}
}
